/**
 * Copyright (C) 2010 MOBICOM. All rights reserved.
 * 
 * 
 * This software is the confidential and proprietary information of
 * Mobicom or one of its subsidiaries. You shall not disclose this
 * confidential information and shall use it only in accordance with
 * the terms of the license agreement or other applicable agreement
 * you entered into with Mobicom.
 * 
 * MOBICOM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. MOBICOM
 * SHALL NOT BE LIABLE FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 * ITS DERIVATIVES.
 * 
 * Apr 11, 2011 7:43:59 PM
 * 
 * @author Nguyen Dinh Phuoc Duc(ndpduc@gmail.com)
 */
package com.lily.smsbox.screen;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.provider.Telephony.Sms;
import com.lily.mysms.R;
import com.lily.smsbox.model.SmsMessage;

/**
 * Internal Class
 * 
 * @author Nguyen Dinh Phuoc Duc (ndpduc@gmail.com)
 */
public class ThreadActivity extends Activity {
  private int _threadId;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thread);
    _threadId = getIntent().getIntExtra("threadid", 0);
  }

  @Override
  protected void onResume() {
    super.onResume();
    reloadMessages();
  }

  private void reloadMessages() {
    Cursor c =
        Sms.query(getContentResolver(), null, Sms.THREAD_ID + "="
            + _threadId, Sms.DATE);
    List<SmsMessage> messages = new ArrayList<SmsMessage>();
    while (c.moveToNext()) {
      SmsMessage message = new SmsMessage();
      message.body = c.getString(c.getColumnIndex(Sms.BODY));
      message.type = c.getInt(c.getColumnIndex(Sms.TYPE));
      message.address = c.getString(c.getColumnIndex(Sms.ADDRESS));
      messages.add(message);
    }

    ArrayAdapter<SmsMessage> adapter =
        new ArrayAdapter<SmsMessage>(this, 0, messages) {
          @Override
          public View getView(int position, View convertView,
              ViewGroup parent) {
            SmsMessage msg = getItem(position);
            if (convertView == null) {
              convertView =
                  LayoutInflater.from(getContext()).inflate(
                      R.layout.li_message,
                      null);
            }
            TextView text =
                (TextView) convertView.findViewById(R.id.text1);
            String content = "";
            switch (msg.type) {
            case Sms.MESSAGE_TYPE_INBOX:
              content += msg.address + ": ";
              break;
            default:
              content += "Me: ";
              break;
            }
            content += msg.body;
            text.setText(content);
            return convertView;
          };
        };
    ListView listSms = (ListView) findViewById(R.id.list_message);
    listSms.setDivider(null);
    listSms.setAdapter(adapter);
  }
}
